package com.liyujie.networkrequest.utils.open;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.liyujie.networkrequest.mobile.entity.OpeningResultEntity;
import com.liyujie.networkrequest.mobile.entity.SignatureDtoEntity;
import com.liyujie.networkrequest.utils.InitializeUtils;
import com.liyujie.networkrequest.utils.popup.PopupUtils;
import com.liyujie.networkrequest.utils.serialport.OpenPortUtils;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

public class OpenCabinetUtils {
    /**
     * 开启柜子
     */
    public static void OpenTheCabinet(SignatureDtoEntity signatureDtoEntity){
        if(null != signatureDtoEntity){
            if(signatureDtoEntity.result){
               boolean SuccessOrNot = OpenPortUtils.openDoor(signatureDtoEntity.boxNum);
               if(SuccessOrNot){
                   /**
                    * 关闭弹框
                    */
                   PopupUtils.dismissAwaitTipsPopup();
                   ThreadUtils.runOnUiThread(new Runnable() {
                       @Override
                       public void run() {
                           PopupUtils.showDoorOpenPopup(ActivityUtils.getTopActivity(),signatureDtoEntity.boxNum);
                       }
                   });
               }
               new Thread(new Runnable() {
                    @Override
                    public void run() {
                        openBox(signatureDtoEntity,SuccessOrNot);
                    }
                }).start();
            }else {

                InitializeUtils.getDefaultMaker().show("凭证无效");
            }
        }else {
            InitializeUtils.getDefaultMaker().show("返回数据非法");
        }

    }

    /**
     * 开门结果回调接口
     * 基本参数：
     * taskId-任务id：String 开门任务唯一标识
     * result-执行结果：bool true成功  false失败
     * reason-失败原因：String 可为空
     *
     * 返回值：
     * result-处理结果：String "succ" 代表成功收到
     */
    public static void openBox(SignatureDtoEntity signatureDtoEntity,boolean SuccessOrNot){
        Map<String,Object> loginMessage = new HashMap<>();
        /**
         * 设备信息
         */
        loginMessage.put("facility", DeviceUtils.getAndroidID());
        /**
         * 失败原因：String 可为空
         */
        loginMessage.put("reason","");
        /**
         * -执行结果：bool true成功  false失败
         */
        loginMessage.put("result",SuccessOrNot);
        /**
         * -任务id：String 开门任务唯一标识
         */
        loginMessage.put("taskId",signatureDtoEntity.taskId);
        /**
         *  验证接口
         */
        Observable<ResponseBody> mObservable = InitializeUtils.getAppComponent().getSystemService().openBox(loginMessage);
        mObservable.subscribeOn(Schedulers.io()).subscribe(new Observer<ResponseBody>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ResponseBody responseBody) {
                if(responseBody.contentLength() == 4){
                    InitializeUtils.getDefaultMaker().show("上传成功");
                }
            }

            @Override
            public void onError(Throwable e) {
                LogUtils.d("结果" + e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }

    /**
     * 关门消息推送接口
     * 基本参数：
     * boxNum-箱柜号：String
     * time：关门时间
     *
     * 返回值：
     * result-处理结果：String "succ" 代表成功收到
     * @param boxNum
     * @param OperatingTime
     */
    public static void closeBox(int boxNum,long OperatingTime){
        Map<String,Object> loginMessage = new HashMap<>();
        /**
         * 设备信息
         */
        loginMessage.put("facility", DeviceUtils.getAndroidID());
        /**
         * boxNum-箱柜号：
         */
        loginMessage.put("boxNum",boxNum);
        /**
         * boxNum-箱柜号：
         */
        loginMessage.put("time",OperatingTime);
        /**
         * 上传时间
         */
        LogUtils.d("上传数据" + GsonUtils.toJson(loginMessage));
        /**
         *  验证接口
         */
        Observable<ResponseBody> mObservable = InitializeUtils.getAppComponent().getSystemService().closeBox(loginMessage);
        mObservable.subscribeOn(Schedulers.io()).subscribe(new Observer<ResponseBody>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ResponseBody responseBody) {
                if(responseBody.contentLength() == 4){
                    InitializeUtils.getDefaultMaker().show("关门上传");
                }
            }

            @Override
            public void onError(Throwable e) {
                LogUtils.d("结果" + e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }
}
